Pixel Colours: A Demo

by Gordon Sweet Gordon@gsweet.fsnet.co.uk


Home

Tip Corner

API Corner

Just for fun

Timing Events

QCard Lesson

Menus: Checkmarks

Dissolve Demo

Encryption

Poker Game

Snip Manager

Demos

Newsletter help

Index

Pixel Colours

Here is a fairly simple program that demonstrates the RGB value of colours,. The first option might also help the wife choose a new colour scheme! Since the Graphics Window chosen is restricted to 800 x 600 for simplicity, I have again used variables ux and uy derived from the screen DisplayWidth to set the position of the window with larger displays. The red, green, blue elements are each selected at random from the full values of 0 to 255. A left click will divert the program to [colpix] and [check] to identify the pixel color at that Mouse position, while a right click reverts to [pick] to select fresh colours.

The other option allows you to check the pixels in any BMP file, which is automatically enlarged to fill the window in use. A left click here diverts to [imagepix] and [check], and a right click allows another BMP file to be selected. The method of determining the colour value of a pixel was also originally published by Alyce Watson. The color recognition routine in [check] is of course extremely useful for games, such as detecting if a certain target has been hit.

You will notice when first selecting a BMP file the program defaults to the Root Directory with the opening DefaultDir$, since only you know where BMP files are stored. Once a file has been selected, a note is made of the path using the function filepath$(f$) so that further BMP files can be located quickly in the same Directory.

Whenever possible I now prefer to keep using the same Window by continually closing it and reopening, rather than the complications of attempting to control numerous Windows. This also allows us to use Buttons in the opening Window, which are then no longer needed in the other Windows.

Gordon Sweet -- Gordon@gsweet.fsnet.co.uk


' With gratefull acknowledgements to Alyce Watson
' in providing the pixel check API code.

    ' For vers 3 and 4
    nomainwin
    DefaultDir$ = left$(DefaultDir$,2) + "\"
    dw = DisplayWidth : dh = DisplayHeight
[start]
    ux = 1 : uy = 1
    if dw > 1000 then ux = 120 : uy = 90
    UpperLeftX = ux : UpperLeftY = uy
    WindowWidth = 800 : WindowHeight = 600
    button #p " All Colours", [colours], LL, 250, 150
    button #p " BMP Images ", [image], LL, 450, 150
    button #p " *** QUIT *** ", [quit], LL, 340, 100
    open "Pixel Tests" for graphics_nsb as #p
    #p "trapclose [quit]; font ariel 24 bold"
    #p "down; fill cyan; backcolor cyan; place 150 100"
    #p "\This finds the pixel colour value"
    #p "\of random colours or in Clipart."
    #p "\LEFT click detects pixels, RIGHT"
    #p "\new random colours or BMP file."

    #p "font arial 10 bold; place 200 220; flush"
    h=hwnd(#p) ': playwave "tada"
    wait

[new]
    close #p : goto [start]

[colours]
    gosub [newscr]
    #p "when leftButtonDown [colpix]"
    #p "when rightButtonDown [pick]"

[pick]
    #p "cls; fill black"
    y = 5 : x = 10
    for n = 1 to 88
        #p "place ";x;" ";y
        red = int(rnd(1)*256)
        green = int(rnd(1)*256)
        blue = int(rnd(1)*256)
        c$=str$(red)+" "+str$(green)+" "+str$(blue)
        #p "backcolor ";c$
        #p "boxfilled ";x + 100;" ";y + 20
        #p "backcolor black; color white; place ";x + 110;" ";y + 16
        #p "\";c$ 
        #p "posxy px py"
        y = y + 25
        if py > 550 then x = x + 190 : y = 5
    next n
    #p "flush" : wait

[colpix]
    gosub [check]
    #p "backcolor black; place 220 565" : #p "\";space$(80)
    #p "place 240 565"
    #p "\Red=";r;" Green=";g;" Blue=";b;" Pixcol=";pixcol
    t$=str$(r)+" "+str$(g)+" "+str$(b)
    #p "backcolor ";t$ 
    #p "place 200 554"
    #p "boxfilled 230 570"
    wait

[image]
    gosub [newscr]
    #p "when leftButtonDown [imagepix]"
    #p "when rightButtonDown [newpic]"
[newpic]
    filedialog "Any BMP file","*.bmp",bmp$
    if bmp$ = "" then [new]
    loadbmp "drawing",bmp$
    DefaultDir$ = filepath$(bmp$)
    #p "cls; background drawing";
    #p "drawsprites; flush"
    wait

[imagepix]
    gosub [check]
    #p "place 1 10" : #p "\";space$(80)
    #p "color black; place 1 10"
    #p "\    Red=";r;" Green=";g;" Blue=";b;" Pixcol=";pixcol
    #p "color ";r;" ";g;" ";b
    #p "size 12; set 8 5"
    wait

[newscr]
    close #p
    tx$ = "LEFT click detects pixels, RIGHT new colours/BMP."
    UpperLeftX = ux : UpperLeftY = uy
    WindowWidth = 800 : WindowHeight = 600
    open tx$ for graphics_nsb as #p
    #p "trapclose [new]; font arial 10 bold; down"
    h=hwnd(#p) : return

[check]
    xp=MouseX : yp=MouseY
    ' get pixel color at x & y
    calldll #user32,"GetDC", h as word, hdc as word
    calldll #gdi32,"GetPixel",_
        hdc as ulong,_
        xp as ulong,_
        yp as ulong,_
        pixcol as ulong

    CallDll #user32, "ReleaseDC",_
        hw as word,_ 'handle of graphic box
        hdc as word,_ 'dc from GetDC
        result as ushort 'returns nonzero if successful

    b = int(pixcol / (256*256))
    g = int((pixcol - b *256*256) / 256)
    r = int(pixcol - b*256*256 - g*256)
    return

[quit]
    close #p : end

function filepath$(f$)
    fl = len(f$)
    for n = fl to 1 step -1
        if mid$(f$,n,1)= "\" then
            pl = n
            exit for
        end if
    next n
    filepath$ = left$(f$,pl)
end function